package com.imooc.mymall.config;

import com.imooc.mymall.intercepter.RateLimitIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * <h1>Web配置类</h1>
 * @Author: crowsjian
 * @Date: 2020/7/15 19:33
 */
@Configuration
@EnableWebMvc
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    /*全局限流拦截器*/
    @Resource
    private RateLimitIntercepter rateLimitIntercepter;

    /**
     * <h2>向Web中添加拦截器</h2>
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置限流拦截器，拦截所有以/api/开头的请求
        registry.addInterceptor(rateLimitIntercepter)
                .addPathPatterns("/api/**");
    }

    /**
     * <h2>静态资源配置</h2>
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置本地文件夹目录映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:C:/Users/Lijian/Desktop/personal/my-mall/uploads/");
        // Swagger2 做的映射
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
